<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Loops
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial024.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial026.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc53">4.2</A>&nbsp;&nbsp;Loops</H2>
<A NAME="sec:loops"></A>
To reduce the need for auxiliary recursive predicates, ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> allows
the use of an iteration construct
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
( IterationSpecs do Goals )
</PRE></BLOCKQUOTE>
Typical applications are: Iteration over a list
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( foreach(X,[1,2,3]) do writeln(X) )
1
2
3
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
Process all elements of one list and construct another:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( foreach(X,[1,2,3]), foreach(Y,List) do Y is X+3 ).
List = [4, 5, 6]
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
Process a list to compute the sum of its elements:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( foreach(X,[1,2,3]), fromto(0,In,Out,Sum) do Out is In+X ).
Sum = 6
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
Note that the variables X, Y, In and Out are local variables in the loop,
while the input list and Sum are shared with the context.<BR>
<BR>
If a parameter remains constant across all loop iterations it must
be specified explicitly (via <B>param</B>),
for example when iterating over an array:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- Array = [](4,3,6,7,8),
   (
       for(I,1,5),
       fromto(0,In,Out,Sum),
       param(Array)
   do
       Out is In + Array[I]
   ).
</PRE></BLOCKQUOTE>

	<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
	<DIV CLASS="center">
	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#DB9370">
	
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>fromto(First,In,Out,Last)</B><DD CLASS="dd-description"><BR>
iterate Goals starting with In=First until Out=Last.
<DT CLASS="dt-description"><B>foreach(X,List)</B><DD CLASS="dd-description"><BR>
iterate Goals with X ranging over all elements of List.
<DT CLASS="dt-description"><B>foreacharg(X,StructOrArray)</B><DD CLASS="dd-description"><BR>
iterate Goals with X ranging over all arguments of StructOrArray.
<DT CLASS="dt-description"><B>foreacharg(X,StructOrArray,Idx)</B><DD CLASS="dd-description"><BR>
same as before, but Idx is set to the argument position of X in
 StructOrArray.
<DT CLASS="dt-description"><B>foreachelem(X,Array)</B><DD CLASS="dd-description"><BR>
like foreacharg/2, but iterates over all elements of an array
 of arbitrary dimension.
<DT CLASS="dt-description"><B>foreachelem(X,Array,Idx)</B><DD CLASS="dd-description"><BR>
same as before, but Idx is set to the index position of X in
 Array.
<DT CLASS="dt-description"><B>foreachindex(Idx,Array)</B><DD CLASS="dd-description"><BR>
like foreachelem/3, but returns just the index position and not the
 element.
<DT CLASS="dt-description"><B>for(I,MinExpr,MaxExpr)</B><DD CLASS="dd-description"><BR>
iterate Goals with I ranging over integers from MinExpr to MaxExpr.
<DT CLASS="dt-description"><B>for(I,MinExpr,MaxExpr,Increment) </B><DD CLASS="dd-description"><BR>
same as before, but Increment can be specified (it defaults to 1). 
<DT CLASS="dt-description"><B>multifor(List,MinList,MaxList)</B><DD CLASS="dd-description"><BR>
like for/3, but allows iteration over multiple indices (saves
 writing nested loops).
<DT CLASS="dt-description"><B>multifor(List,MinList,MaxList,IncrementList)</B><DD CLASS="dd-description"><BR>
same as before, but IncrementList can be specified (i.e. how
 much to increment each element of List by).
<DT CLASS="dt-description"><B>count(I,Min,Max)</B><DD CLASS="dd-description"><BR>
iterate Goals with I ranging over integers from Min up to Max.
<DT CLASS="dt-description"><B>param(Var1,Var2,...)</B><DD CLASS="dd-description"><BR>
for declaring variables in Goals global, i.e. shared with the context.
</DL>

	</TD>
</TR></TABLE>
	</DIV>
	<BR>
<BR>
<DIV CLASS="center">Figure 4.1: Iteration Specifiers for Loops</DIV><BR>
<BR>

	<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>

<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For details and more examples see the description of the
</FONT><A HREF="../bips/kernel/control/do-2.html"><FONT COLOR="#9832CC"><B>do/2</B></FONT></A><A NAME="@default84"></A><FONT COLOR="#9832CC">
built-in predicate. Additional background can be found in [</FONT><A HREF="tutorial133.html#loops02"><CITE><FONT COLOR="#9832CC">24</FONT></CITE></A><FONT COLOR="#9832CC">].</FONT>
</DL>

<HR>
<A HREF="tutorial024.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial026.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
